package proeva;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Report_DDoS_W_Servlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Report_DDoS_W_Servlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		StringBuffer data = new StringBuffer("");
		String json;
		
		if( request.getParameter("UorS").equalsIgnoreCase("u") )//UDP
		{
			try
			{
				Connection Conn = DBSetting.GetEvaConnection();
				Statement stmt = Conn.createStatement();
				ResultSet result= stmt.executeQuery("SELECT * FROM DDoS_Eva_W WHERE Type='true' ORDER BY Eva_Time DESC");
				while( result.next() )
				{
					data.append("{Eva_Time:");
					data.append(result.getTimestamp("Eva_Time"));
					
					data.append(",Lp:");
					data.append(result.getDouble("Lp"));

					data.append(",Bw:");
					data.append(result.getDouble("Bw"));

					data.append(",Lt:");
					data.append(result.getDouble("Lt"));

					data.append(",Ia:");
					data.append(result.getDouble("Ia"));

					data.append(",Ir:");
					data.append(result.getDouble("Ir"));

					data.append(",Eva:");
					data.append(result.getDouble("Eva"));

					data.append("},");
				}
				if( data.length() != 0 )
					data.deleteCharAt(data.length() - 1);

				json = "{success:true,msg:'查询数据库成功',data:{"+data+"}}";
				response.getWriter().write(json);

				result.close();
				stmt.close();
				Conn.close();
			} catch (Exception ex) {
				json = "{success:false,msg:'查询数据库时出错，请重试',data:{}}";
				out.write(json);
				out.write(ex.getMessage());
			}
		}
		else//SYN
		{
			try
			{
				Connection Conn = DBSetting.GetEvaConnection();
				Statement stmt = Conn.createStatement();
				ResultSet result= stmt.executeQuery("SELECT * FROM DDoS_Eva_W WHERE Type='false' ORDER BY Eva_Time DESC");
				while( result.next() )
				{
					data.append("{Eva_Time:");
					data.append(result.getTimestamp("Eva_Time"));
					
					data.append(",Lp:");
					data.append(result.getDouble("Lp"));

					data.append(",Bw:");
					data.append(result.getDouble("Bw"));

					data.append(",M:");
					data.append(result.getInt("M"));

					data.append(",synSa:");
					data.append(result.getDouble("synSa"));

					data.append(",synDelay:");
					data.append(result.getDouble("synDelay"));

					data.append(",Lt:");
					data.append(result.getDouble("Lt"));

					data.append(",Ia:");
					data.append(result.getDouble("Ia"));

					data.append(",Ir:");
					data.append(result.getDouble("Ir"));
					
					data.append(",Ti:");
					data.append(result.getDouble("Ti"));

					data.append(",Eva:");
					data.append(result.getDouble("Eva"));

					data.append("},");
				}
				if( data.length() != 0 )
					data.deleteCharAt(data.length() - 1);

				json = "{success:true,msg:'查询数据库成功',data:{"+data+"}}";
				response.getWriter().write(json);

				result.close();
				stmt.close();
				Conn.close();
			} catch (Exception ex) {
				json = "{success:false,msg:'查询数据库时出错，请重试',data:{}}";
				out.write(json);
				out.write(ex.getMessage());
			}
		}
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet( request, response );
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
